<template>
    <div>
        <el-card>
            <el-table :data="orders">
                <el-table-column label="id">
                    <template scope="scope">
                        <span>{{scope.row.id}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="用户">
                    <template scope="scope">
                        <el-input v-if="scope.row.EdtiStatus" size="small" v-model="temp.user_name" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.user_name}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="订单号">
                    <template scope="scope">
                        <el-input v-if="scope.row.EdtiStatus" size="small" v-model="temp.order_sn" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.order_sn}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="金额">
                    <template scope="scope">
                        <el-input v-if="scope.row.EdtiStatus" size="small" v-model="temp.pay_amount" placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.pay_amount}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="支付状态">
                    <template scope="scope">
                        <el-select v-if="scope.row.EdtiStatus" v-model="temp.status" placeholder="选择状态"
                                   style="margin-left: 50px">
                            <el-option :label="c" :value="c" v-for="c in categories"></el-option>
                        </el-select>
                        <span v-else>{{categories[scope.row.status]}}</span>
                    </template>
                </el-table-column>
                <el-table-column label="下单日期">
                    <template scope="scope">
                        <el-input v-if="scope.row.EdtiStatus" size="small" v-model="temp.create_time"
                                  placeholder="请输入内容"
                        ></el-input>
                        <span v-else>{{scope.row.create_time}}</span>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="120">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.EdtiStatus" @click="confirmEdit(scope.$index,scope.row)" type="text"
                                   size="small">确认
                        </el-button>
                        <el-button v-else type="text" size="small" @click="orderDelete(scope.row)">删除</el-button>
                        <el-button v-if="scope.row.EdtiStatus" type="text" size="small"
                                   @click="cancelEdit(scope.$index, scope.row)">取消编辑
                        </el-button>
                        <el-button type="text" size="small" @click="edit(scope.$index, scope.row)" v-else>编辑</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "PermissionManager",
        data() {
            return {
                orders: [],
                temp: [],
                insert: {},
                dialogTableVisible: false,
                dialogFormVisible: false,
                categories: ["待支付", "已支付", "订单已关闭"]
            }
        },
        mounted: function () {
            this.init();
        },
        methods: {
            init() {
                this.loadOrders();
            },
            // permissionAdd() {
            //
            //     this.$http.post("/permission/insertPermission", this.insert).then((res) => {
            //         if (res.data.code == 0) {
            //             this.$msg.success(res.data.message)
            //             this.dialogFormVisible = false
            //             this.loadOrders();
            //         }
            //     })
            // },
            loadOrders() {
                this.$http.get("/order/selectAllOrder").then((res) => {
                    if (res.data.code == 0) {
                        res.data.data.forEach((x) => {
                                x.EdtiStatus = false;
                            }
                        )
                        this.orders = res.data.data

                    }
                })
            },
            handleEdit(index, row) {
                console.log(index, row);
                row.EdtiStatus = false;
                this.$set(this.orders, index, row)
            },
            orderDelete(row) {
                this.$http.get("/order/deleteOrder", {
                    params: {id: row.id}
                }).then((res) => {
                    if (res.data.code == 0) {
                        this.$msg.success(res.data.message + '请刷新页面')
                        this.loadOrders();
                    } else {
                        this.$msg.fail(res.data.message)
                    }
                })
            },
            edit(index, row) {
                this.temp = JSON.parse(JSON.stringify(row))
                row.EdtiStatus = true;
                this.$set(this.orders, index, row)
            },
            cancelEdit(index, row) {
                row.EdtiStatus = false;
                this.$set(this.orders, index, row)
            },
            confirmEdit(index, row) {
                row = JSON.parse(JSON.stringify(this.temp))
                this.$set(this.orders, index, JSON.parse(JSON.stringify(this.temp)))
                row.EdtiStatus = false;
                this.temp.status = this.categories.indexOf(this.temp.status)
                this.$http.post("/order/updateOrder", this.temp).then((res) => {
                    if (res.data.code == 0) {
                        this.$msg.success("更新成功");
                        this.loadOrders();
                    }
                })
            }
        }
    }
</script>

<style scoped>

</style>